<?php

namespace common\models\project;

use Yii;
use common\models\projectcategory\ProjectCategory;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "project".
 *
 * @property integer $id
 * @property string $name
 * @property string $cname
 * @property integer $category
 * @property string $management
 * @property string $bearunit
 * @property integer $starttime
 * @property integer $endtime
 * @property string $director
 * @property integer $funds
 * @property integer $surplus
 * @property integer $status
 */
class Project extends \yii\db\ActiveRecord
{
    const STATUS_BEFORE = 0;//未开始
    const STATUS_ACTIVE = 1;//进行中
    const STATUS_INACTIVE = 2;//已结束
    private $_statusLabel;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'project';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['category','funds', 'surplus', 'status'], 'integer'],
            [['name', 'cname', 'management', 'bearunit', 'director', 'file', 'filename'], 'string', 'max' => 255],
            [['starttime', 'endtime'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'name' => Yii::t('app', '项目名称'),
            'cname' => Yii::t('app', '子项目名称'),
            'category' => Yii::t('app', '项目类别'),
            'management' => Yii::t('app', '项目归口管理部门'),
            'bearunit' => Yii::t('app', '项目承担单位'),
            'starttime' => Yii::t('app', '开始年限'),
            'endtime' => Yii::t('app', '结束年限'),
            'director' => Yii::t('app', '项目负责人'),
            'funds' => Yii::t('app', '项目经费'),
            'surplus' => Yii::t('app', '项目剩余经费'),
            'status' => Yii::t('app', '进行状态'),
            'file' => Yii::t('app', '项目文件'),
            'filename' => Yii::t('app', '项目文件名'),
        ];
    }

    
    public static function getArrayCategory()
    {
        return ArrayHelper::map(ProjectCategory::find()->all(), 'id', 'name');
    }


    public function getStatusLabel()
    {
        if ($this->_statusLabel === null) {
            $statuses = self::getArrayStatus();
            $this->_statusLabel = array_key_exists($this->status,self::getArrayStatus()) ? $statuses[$this->status] : '无';
        }
        return $this->_statusLabel;
    }
    /**
     * @inheritdoc
     */
    public static function getArrayStatus()
    {
        return [
            self::STATUS_BEFORE => Yii::t('app', '未进行'),
            self::STATUS_ACTIVE => Yii::t('app', '进行中'),
            self::STATUS_INACTIVE => Yii::t('app', '已结束'),
        ];
    }

}
